import 'dart:io';
import 'package:appinio_video_player/appinio_video_player.dart';
import 'package:flutter/material.dart';
import 'package:flutter_project/config/app_bar_view.dart';

class VideoPlayerView extends StatefulWidget {
  final String path;

  const VideoPlayerView({this.path = '', super.key});

  @override
  State<VideoPlayerView> createState() => _VideoPlayerViewState();
}

class _VideoPlayerViewState extends State<VideoPlayerView> {
  late VideoPlayerController _controller;
  late CustomVideoPlayerController _customVideoPlayerController;

  @override
  Widget build(BuildContext context) {
    return CommonAppWidget.navAppBar(
      title: '详情',
      childWidget: Material(
        child: Center(
          child: CustomVideoPlayer(
            customVideoPlayerController: _customVideoPlayerController,
          ),
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    String path = widget.path;
    if (path.startsWith('http')) {
      _controller = VideoPlayerController.networkUrl(Uri.parse(path))
        ..initialize().then((_) {
          if (!mounted) return;
          setState(() {});
        });
    } else {
      _controller = VideoPlayerController.file(File(path))
        ..initialize().then((_) {
          if (!mounted) return;
          setState(() {});
        });
    }

    _customVideoPlayerController = CustomVideoPlayerController(
      context: context,
      videoPlayerController: _controller,
      customVideoPlayerSettings: const CustomVideoPlayerSettings(
        showPlayButton: true,
        showFullscreenButton: true,
        settingsButtonAvailable: true,
        autoFadeOutControls: false,
      ),
    );
    _controller.play();
  }

  @override
  void dispose() {
    _controller.pause();
    _controller.dispose();
    super.dispose();
  }
}
